// 初始样式
require('../../assets/css/index.less');
require('../../assets/css/basic.less');
// 小图标
require('../../assets/fonts/iconfont.css');
require('../../assets/font/iconfont.css');
// 引入weui
require("../../lib/weui/weui.css");
const { default: axios } = require('axios');
var weui = require("../../lib/weui/weui.js");
// 当前页面样式
require('./sportData.less');
// 工具函数 utils
const dom = require('../../utils/dom');
const https = require('../../utils/https');
const local = require('../../utils/local');
// 引入echarts
import * as echarts from 'echarts';

dom.ready(function() {

    function getEchar(xdata, ydata) {
        //  柱状图
        var myEcharts = echarts.init(dom.getEle(".chart1"));
        var option = {
            title: {
                text: "近7天运动时长"
            },
            xAxis: {
                type: 'category',
                data: xdata,
                axisLabel: {
                    rotate: 45,
                },
                axisTick: {
                    "alignWithLabel": true
                }



            },
            yAxis: {
                type: 'value'
            },
            series: [{
                data: ydata,
                type: 'bar',
                showBackground: true,
                backgroundStyle: {
                    color: 'rgba(180, 180, 180, 0.2)'
                }
            }]
        };
        myEcharts.setOption(option);

    }

    // 获取数据
    https.get("/api/exercise", function(res) {
        var { errno, data } = res;
        if (errno == 0) {
            console.log(data);
            var xda = data.days.map(v => {
                // console.log(v);
                return v.date.slice(5);
            })
            var yda = data.days.map(v => {
                    return v.sumDuration
                })
                // console.log(xda, yda);
            getEchar(xda, yda);
        }
    })

    //  饼状图
    var myEcharts2 = echarts.init(dom.getEle(".chart2"));
    var option2 = {
        title: {
            text: 'Referer of a Website',
            subtext: 'Fake Data',
            left: 'center'
        },
        tooltip: {
            trigger: 'item'
        },
        legend: {
            orient: 'vertical',
            left: 'left'
        },
        series: [{
            name: 'Access From',
            type: 'pie',
            radius: '50%',
            data: [
                { value: 1048, name: '骑行' },
                { value: 735, name: '跑步' },
                { value: 580, name: '训练' },
            ],
            emphasis: {
                itemStyle: {
                    shadowBlur: 10,
                    shadowOffsetX: 0,
                    shadowColor: 'rgba(0, 0, 0, 0.5)'
                }
            }
        }]
    };
    myEcharts2.setOption(option2);


    // 返回
    dom.addEvent(".backBtn", "click", function() {
        history.go(-1);
    })
})